<template>
  <div>
    <Header :title="title"></Header>
    <van-form
      :show-error='false'
      :colon="true"
      :validate-first='true'
      @submit="onLogin"
      class="form"
    >
      <van-field
        class="field"
        v-model="mobile"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        :rules="formRules.phone"
      />
      <van-field
        class="field"
        v-model="code"
        name="验证码"
        label="验证码"
        placeholder="请输入验证码"
        :rules="formRules.code"
      >
        <template #button>
          <van-button size="mini" class="smsbtn" @click.prevent="sendSms" :disabled="isSend" round>{{count?`${count}s`:'发送验证码'}}</van-button>
        </template>
      </van-field>
        <div>
          <van-button round block class="button" type="info" native-type="submit">登录</van-button>
        </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from 'vant';
import Header from '../../components/Header/Header.vue'
import {login,code} from '../../api/user'
export default {
  name: "Login",
  created() {},
  data() {
    return {
      title: '登录', // 头部导航栏名字
      mobile: '',  //手机号
      code: '',  //验证码
      errorMessage:'',  //错误提示
      formRules:{
        mobile:[{ required: true, message: '请填写手机号' },
        { pattern: /^1[3|5|7|8|9]\d{9}$/, message: '手机号格式错误' }
        ],
        code:[{ required: true, message: '请填写验证码' },
        { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      count:0, // 发送倒计时
      isSend:false  // 发送后禁用
    }
  },
  methods: {
    async onLogin(){
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        // 值为0是不会关闭
        duration: 0
      })
    try {
      const {data} = await login({mobile:this.mobile,code:this.code})
      console.log(data)
      this.$store.commit('setUser',data.data)
      this.$toast.success('登陆成功')
      this.$router.replace('/my')
    } catch (error) {
        Toast.fail('登录失败'+error)
    }
    },
    // 发送验证码
    async sendSms(){
       if (/^1[3|5|7|8|9]\d{9}$/.test(this.mobile)) {
        if (!this.count) {
          clearInterval(this.intervalId)
          try {
            this.isSend = true
            const data = await code(this.mobile)
            // console.log(data)
          } catch (error) {
            // console.log(error.response)
            if (error && error.response && error.response.status === 429) {
              this.$toast.fail('发送太频繁了，请稍后重试')
              this.isSend = false
              return
            }
          }
          this.count = 60
          this.intervalId = setInterval(() => {
            this.count--
            if (this.count === 0) {
              clearInterval(this.intervalId)
              this.isSend = false
            }
          }, 1000)
        }
        }else{
            this.$toast({
              message: '手机号错误',
              forbidClick: true,
              position: 'top'
            })
          }
        }},
  props: {},
  components:{
    Header
    }
}
</script>

<style lang="less" scoped>
// @import '../../styles/index.less';

/deep/.form{
  padding: 10px;
  .field{
    height: 58px;
    border-bottom: 1px solid #eeeeee;
    // .van-field__label{
    //   margin-left: 50px;
    // }
    .van-field__value{
      margin-left: -30px;
    }
    .smsbtn{
      background-color: #ededed;
    }
  }
}
.button{
  border-radius: 6px;
  margin-top: 10px;
}
.button:active{
  border-radius: 6px;
  transition: opacity 0.6s ease;
  opacity: 0.3;
}
</style>
